package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.Contact;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class ContactDAO extends AbstractDao<Contact>{
	
	private Connection con = this.connect;
	public static final String DELETE_SQL = "DELETE FROM oce_contact WHERE contact_Id = ?";
	public static final String SELECT_ALL_SQL = "select * from oce_contact";
	public static final String SELECT_SQL = "select * from oce_contact where contact_Id = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_contact (contact_Id,contact_name,phone,fax,email,website) VALUES (?,?,?,?,?,?)";
	public static final String UPDATE_SQL = "UPDATE oce_contact SET contact_name = ?, phone = ?, fax = ?, email = ? , website = ? WHERE contact_Id = ? ";
	
	 
	
	public Contact create(Contact obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);

			long id = 0l;
			ps.setLong(1,id);
			ps.setString(2, obj.getName());
			ps.setString(3, obj.getPhone());
			ps.setString(4, obj.getFax());
			ps.setString(5, obj.getEmail());			
			ps.setString(6, obj.getWebsite());						
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public Contact update(Contact obj){
		try{
			
			if(obj == null || obj.getId() == null || obj.getId().equals(""))
				return obj;
						
			PreparedStatement ps = con.prepareStatement(UPDATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(1, obj.getName());
			ps.setString(2, obj.getPhone());
			ps.setString(3, obj.getFax());
			ps.setString(4, obj.getEmail());
			ps.setString(5, obj.getWebsite());
			ps.setString(6, obj.getId());
						
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public Contact find(long id){
		List<Contact> contacts = new ArrayList<Contact>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			contacts = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return contacts.size()== 0 ? null : contacts.get(0);
	}
	
	public List<Contact> findAll(){
		List<Contact> contacts = new ArrayList<Contact>();
		try{
			contacts = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			contacts = null;
		}
		return contacts;
	}
	
	public void delete(Contact obj){
		try{
			PreparedStatement ps = con.prepareStatement(DELETE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,Long.parseLong(obj.getId()));
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();		
		}catch (Exception e) {
			obj = null;
		}
	}
	
	private List<Contact> convert(ResultSet rs) throws SQLException {
		List<Contact> contacts = new ArrayList<Contact>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        String phone = rs.getString(3);
	        String fax = rs.getString(4);
	        String email = rs.getString(5);
	        String website = rs.getString(6);
	        contacts.add(new Contact(id,name,phone,fax,email,website));
	    }
		rs.close();
		return contacts;
	}
}
